package com.shy.base.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VerifyImgUtil{

    // 设置字母的大小,大小
    private Font mfont = new Font("Times New Roman", Font.PLAIN, 17);
    // 随机验证码内容
    private char[] ch = "0123456789".toCharArray();
    private char[] op = "+-*".toCharArray();
    // 设置验证码位数
    private int checkSum = 2;

    Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int rgbR = fc + random.nextInt(bc - fc);
        int rgbG = fc + random.nextInt(bc - fc);
        int rgbB = fc + random.nextInt(bc - fc);
        return new Color(rgbR, rgbG, rgbB);
    }

    /**
     * 输出驗證碼方法
     */
    public void getRandcode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 表明生成的响应是图片
        response.setContentType("image/jpeg");

        int width = 70;
        int height = 24;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics graphics = image.getGraphics();
        Random random = new Random();
        graphics.setColor(getRandColor(200, 250));
        graphics.fillRect(1, 1, width - 1, height - 1);
        graphics.setColor(new Color(102, 102, 102));
        graphics.drawRect(0, 0, width - 1, height - 1);
        graphics.setFont(mfont);

        graphics.setColor(getRandColor(160, 200));

        // 画随机线
        for (int i = 0; i < 155; i++) {
            int xs = random.nextInt(width - 1);
            int ys = random.nextInt(height - 1);
            int xl = random.nextInt(6) + 1;
            int yl = random.nextInt(12) + 1;
            graphics.drawLine(xs, ys, xs + xl, ys + yl);
        }

        // 从另一方向画随机线
        for (int i = 0; i < 70; i++) {
            int xs = random.nextInt(width - 1);
            int ys = random.nextInt(height - 1);
            int xl = random.nextInt(12) + 1;
            int yl = random.nextInt(6) + 1;
            graphics.drawLine(xs, ys, xs - xl, ys - yl);
        }

        // 生成随机数和操作符拼接成表达式
        String strRand = "";
        int firstNum = 0;
        for (int i = 0; i < checkSum; i++) {
            // 默认最后一个字符为=
            String otmp = "=?";
            int itmp;
            if (i != checkSum - 1) {
                // 非最后一数字从0-9
                itmp = random.nextInt(8) + 1;
                int jtmp = random.nextInt(2);
                otmp = String.valueOf(op[jtmp]);
                // 为保证减法正确性，第二个数小于等于第一个数
                firstNum = otmp.equals("-") ? itmp : 9;
            } else {
                itmp = random.nextInt(firstNum);
            }
            char ctmp = ch[itmp];
            strRand += String.valueOf(ctmp);
            strRand += otmp;
            graphics.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110),
                    20 + random.nextInt(110)));
            graphics.setFont(new Font("Arial", Font.BOLD, 20));
            graphics.drawString(String.valueOf(ctmp), 20 * i + 10, 20);
            graphics.drawString(otmp, 20 * (i + 1), 20);
        }

        HttpSession session = request.getSession(true);
        // 表达式运算时应去除=？
        session.setAttribute("exprand", strRand.replaceAll("=\\?", ""));
        graphics.dispose();
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }

}
